package com.lyy.photovoltaic.service.impl;

import com.lyy.photovoltaic.beans.UsersInfo;
import com.lyy.photovoltaic.common.vo.ResultCode;
import com.lyy.photovoltaic.common.vo.ResultVO;
import com.lyy.photovoltaic.mapper.UsersInfoMapper;
import com.lyy.photovoltaic.service.UsersInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
public class UsersInfoServixeImpl implements UsersInfoService {
    @Autowired
    UsersInfoMapper usersInfoMapper;
    @Override
    public ResultVO findUserInfoById(String uIId) {
        UsersInfo userInfo = usersInfoMapper.selectByPrimaryKey(uIId);
//        System.out.println(userInfo);
        ResultVO resultVO = new ResultVO(ResultCode.SUCCESS, "查询用户信息成功", userInfo);
        return resultVO;
    }
    public ResultVO findUserInfoByUId(String uId) {
//        System.out.println(uId);
        Example example = new Example(UsersInfo.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("userId",uId);
        List<UsersInfo> usersInfos = usersInfoMapper.selectByExample(example);
//        System.out.println(userInfo);
        ResultVO resultVO = new ResultVO(ResultCode.SUCCESS, "查询用户信息成功", usersInfos);
        return resultVO;
    }

    @Override
    public ResultVO findAllUserInfo() {
        List<UsersInfo> usersInfos = usersInfoMapper.selectAll();
        if (usersInfos.size() > 0) {
            ResultVO resultVO = new ResultVO(ResultCode.SUCCESS, "查询所有用户信息成功", usersInfos);
            return resultVO;
        }
        return new ResultVO(ResultCode.FAILED, "查询用户信息失败",null);
    }

    @Override
    public ResultVO updateUserInfo(UsersInfo userInfo) {
//        System.out.println("查看前端传输的数据");
//        System.out.println(userInfo);
        int i = usersInfoMapper.updateByPrimaryKey(userInfo);
        System.out.println(i);
        if (i > 0) {
            return new ResultVO(ResultCode.SUCCESS, "更新用户信息成功", userInfo);
        }
        return new ResultVO(ResultCode.FAILED, "更新用户信息失败",null);
    }
}
